﻿using System;
using System.Collections.Generic;
using System.Linq;
using ECommon.Components;
using Infrastructure;
using QueryServices.DTOs;

namespace QueryServices.Ef
{
    [Component]
    public class SectionQueryService : BaseQueryService, ISectionQueryService
    {
        public IEnumerable<SectionInfo> FindAll()
        {
            using (Repository.EntityFramework.Forums.DataContext db = new Repository.EntityFramework.Forums.DataContext())
            {
                var s = db.Section.Select(x => new SectionInfo
                {
                    Id = x.Id,
                    Name = x.Name
                }).ToList();
                return s;
            }
        }
        public dynamic FindDynamic(string id, string option)
        {
            if (option == "simple")
            {
                using (Repository.EntityFramework.Forums.DataContext db = new Repository.EntityFramework.Forums.DataContext())
                {

                    var s = db.Section.AsNoTracking().Where(x => x.Id == id).Select(x => new
                    {
                        id = x.Id,
                        name = x.Name
                    }).SingleOrDefault();
                    return s;
                }
            }
            throw new Exception("Invalid find option:" + option);
        }
    }
}
